package com.practise.rabbitmqdemo.controller;

import com.alibaba.fastjson.JSON;
import com.practise.rabbitmqdemo.config.TTLDirectRabbitConfig;
import com.practise.rabbitmqdemo.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;

@Slf4j
@RestController
@RequestMapping(value = "ttl/direct")
@Api(value = "send DirectMsg controller", tags = {"ttl消息队列接口"})
public class TTLDirectMsgController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @ApiOperation(value = "添加用户到TTLQ1", httpMethod = "POST")
    @RequestMapping(value = "sendToTTLQueue1")
    public void add(@RequestBody @ApiParam(name = "User", value = "测试生成类", required = true) User user) {
        log.info("add user to routing1");
        rabbitTemplate.convertAndSend(TTLDirectRabbitConfig.TTL_EXCHANGE_1,TTLDirectRabbitConfig.ROUTING_1, JSON.toJSONString(user));
        return;
    }

    @ApiOperation(value = "添加消息到Q2", httpMethod = "POST")
    @RequestMapping(value = "sendToQueue2")
    public String sendToQueue(String userName, String msg) throws ExecutionException, InterruptedException {
        String concatMsg = "hi,".concat(userName).concat(",").concat(msg);
        rabbitTemplate.convertAndSend(TTLDirectRabbitConfig.TTL_EXCHANGE_1,TTLDirectRabbitConfig.ROUTING_1,
                "testTTL 15s",
                message ->{
                    message.getMessageProperties().setExpiration("15000");
                    message.getMessageProperties().setContentEncoding("utf-8");
                    return message;
                });
        return concatMsg;
    }

    @ApiOperation(value = "添加消息到Q2 confirm", httpMethod = "POST")
    @RequestMapping(value = "sendToQueue22")
    public String sendToQueue22(String userName, String msg) throws ExecutionException, InterruptedException {
        String concatMsg = "hi,".concat(userName).concat(",").concat(msg);
        CorrelationData correlationData = new CorrelationData();
        rabbitTemplate.convertAndSend(TTLDirectRabbitConfig.TTL_EXCHANGE_1,TTLDirectRabbitConfig.ROUTING_1,
                "testTTL 15s",
                message ->{
                    message.getMessageProperties().setExpiration("15000");
                    message.getMessageProperties().setContentEncoding("utf-8");
                    return message;
                }
                ,correlationData);
        CorrelationData.Confirm confirm = correlationData.getFuture().get();
        log.info("confirm = {}",confirm);
        return concatMsg;
    }

}
